package com.hexb.smh.utils;

import com.hexb.core.exception.BusinessException;
import com.hexb.core.utils.Assert;
import com.hexb.core.utils.RSAUtils;
import com.hexb.smh.errors.ErrorCodes;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author : hexb
 */
public interface IPasswordIn {

    @ApiModelProperty(hidden = true)
    default String getRawPassword() {
        String pwd = getPassword();
        if (!getEncrypt()) return pwd;
        try {
            return RSAUtils.decryptByPrivateKey(pwd, SmhConstant.RSA_PRIVATE);
        } catch (Exception e) {
            throw new BusinessException(ErrorCodes.INVALID_ENCRYPT_CONTENT);
        }
    }

    /**
     * 是否包含字母和数据
     */
    static Boolean isIncludeLetterAndDigit(String pwd) {
        return pwd.matches(".*[\\d]+.*") && pwd.matches(".*[a-zA-Z]+.*");
    }

    static void assertPwd(String pwd) {
        Assert.isFalse(isIncludeLetterAndDigit(pwd), ErrorCodes.INVALID_PASSWORD_LENGTH);
    }

    String getPassword();

    Boolean getEncrypt();
}
